//
//  ServerView.m
//  pa
//
//  Created by HaiJiao Chen on 12-7-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ServerView.h"
#import "LoginServer.h"
#import "FilePack.h"
#import "MultiLanguageButton.h"
#import "XMLObject.h"
#import "ASIHTTPRequest.h"
#import "CHJAlertView.h"
#import "LoginServerBG.h"
#import "KXUIColor.h"
#include "text_def.h"

@implementation ServerView
@synthesize table;
@synthesize server_name;
@synthesize server_address;
@synthesize english_btn;
@synthesize spanish_btn;
@synthesize switch_img;

- (void)bind
{
    [list setupController:self.table data:[LoginServer sharedInstance] reuse:@"loginServers"];
    [self.server_name becomeFirstResponder];
    LoginServerBG* bg = [[LoginServerBG alloc] initWithFrame:CGRectZero];
    [list setBGWithColor1:[UIColor colorWithARGB:0xFFF7F7F7] color2:[UIColor colorWithARGB:0xFFC4CCD6] height:38 bg:bg];
    [bg release];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        normalQuene = [[ASINetworkQueue alloc] init];
        [normalQuene setDelegate:self];
        [normalQuene setRequestDidFinishSelector:@selector(requestFinished:)];
        [normalQuene setRequestDidFailSelector:@selector(requestFailed:)];
        normalQuene.shouldCancelAllRequestsOnFailure = NO;
        [normalQuene go];

        
        list = [[ServerList alloc] init];
        list.selectedIndex = -1;
        list.delegate = self;
    }
    return self;
}
-(void)dealloc
{
    [normalQuene cancelAllOperations];
    [normalQuene setDelegate:nil];
    [normalQuene release];
    [list release];
    [super dealloc];
}
-(IBAction)english:(id)sender
{
    english_btn.enabled = NO;
    spanish_btn.enabled = YES;
    switch_img.image = [UIImage imageNamed:@"switch1.png"];
    [FilePack setCurrentlanguage:@"en-US"];
}
-(IBAction)spanish:(id)sender
{
    english_btn.enabled = YES;
    spanish_btn.enabled = NO;
    switch_img.image = [UIImage imageNamed:@"switch2.png"];
    [FilePack setCurrentlanguage:@"es-MX"];
}
-(IBAction)test:(id)sender
{
    NSString* testUrl = server_address.text;
    if ([testUrl length] > 0)
    {
        NSString* api = [NSString stringWithFormat:@"%@/paperlessapi.asmx/serverConnectionCheck?&Language=%@&sAddress=1", testUrl, [FilePack currentlanguage]];
        NSURL *url = [NSURL URLWithString:api];
        ASIHTTPRequest* request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
        [normalQuene addOperation:request];

        waittingAlertView = [[CHJAlertView alloc] init];
        [waittingAlertView setLoadingText:[FilePack getStringByID:STR_PLEASE_WAIT] animated:YES];
        [waittingAlertView show];
        [waittingAlertView release];
    }
}
-(IBAction)add_server:(id)sender
{
    if ([server_name.text length] != 0 && [server_address.text length] != 0)
    {
        [[LoginServer sharedInstance] addServerWithName:server_name.text andUrl:server_address.text];
    }
}
-(IBAction)cancel:(id)sender
{
    [self removeFromSuperview];
}
- (void)tableView:(UITableView*)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    list.selectedIndex = [indexPath row];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    XMLObject* xml = [XMLObject parse:[request rawResponseData]];
    if ([xml getIntForKey:@"ret" defaultValue:0] == 1)
    {
        [waittingAlertView setTitle:[FilePack getStringByID:STR_SERVER_AVALIABLE] max_width:160 animated:YES];
        [waittingAlertView closeInTime:2];
        waittingAlertView = nil;
    }
    else
    {
        [waittingAlertView setTitle:[xml getStringForKey:@"error" defaultValue:[FilePack getStringByID:STR_SERVER_NOT_AVALIABLE]] max_width:160 animated:YES];
        [waittingAlertView closeInTime:2];
        waittingAlertView = nil;
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    [waittingAlertView setTitle:[FilePack getStringByID:STR_SERVER_NOT_AVALIABLE] max_width:160 animated:YES];
    [waittingAlertView closeInTime:2];
    waittingAlertView = nil;
}
@end


@implementation ServerList
@synthesize selectedIndex;

- (void)setSelectedIndex:(int)_selectedIndex
{
    selectedIndex = _selectedIndex;
    [tableView reloadData];
}

- (void)delete:(id)sender
{
    [[LoginServer sharedInstance] removeServerWithIndex:self.selectedIndex];
    self.selectedIndex = -1;
}
-(void)createSubView:(UITableViewCell*)cell
{
    UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(23, 0, 126, 38)];
    [cell.contentView addSubview:name];
    name.backgroundColor = [UIColor clearColor];
    name.tag = 1;
    [name release];
    
    UILabel* address = [[UILabel alloc] initWithFrame:CGRectMake(164, 0, 450, 38)];
    [cell.contentView addSubview:address];
    address.backgroundColor = [UIColor clearColor];
    address.tag = 2;
    [address release];
    
    MultiLanguageButton* btn = [[MultiLanguageButton alloc] initWithFrame:CGRectMake(533, 4, 72, 30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_delete_bg.png"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.contentView addSubview:btn];
    btn.tag = 3;
    [btn setTextID:STR_DELETE];
    [btn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [btn release];
    
    cell.contentView.backgroundColor = [UIColor clearColor];
}
-(void)setupNewCell:(UITableViewCell*)cell obj:(id<NSObject>)obj indexPath:(NSIndexPath*)indexPath
{
    NSDictionary* dic = (NSDictionary*)obj;
    UILabel* name = (UILabel*)[cell.contentView viewWithTag:1];
    UILabel* address = (UILabel*)[cell.contentView viewWithTag:2];
    MultiLanguageButton* btn = (MultiLanguageButton*)[cell.contentView viewWithTag:3];
    name.text = [dic valueForKey:@"name"];
    address.text = [dic valueForKey:@"url"];
    btn.hidden = self.selectedIndex != [indexPath row];
    if ([dic valueForKey:@"changable"] == nil)
    {
        btn.hidden = YES;
    }
}
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath obj:(id<NSObject>)obj
{
    return 38;
}

@end